import 'dart:async';
import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

/// 通过assets路径，获取资源图片
Future<ui.Image> loadAssetImage1(String asset) async {
  ByteData data = await rootBundle.load(asset);
  ui.Codec codec = await ui.instantiateImageCodec(data.buffer.asUint8List());
  ui.FrameInfo fi = await codec.getNextFrame();
  return fi.image;
}

/// 通过assets路径，获取资源图片
Future<ui.Image> loadAssetImage(String assetUri) async {
  ImageStream imageStream = AssetImage(assetUri).resolve(ImageConfiguration());
  Completer<ui.Image> completer = Completer<ui.Image>();
  void imageListener(ImageInfo info, bool synchronousCall) {
    ui.Image image = info.image;
    completer.complete(image);
    imageStream.removeListener(ImageStreamListener(imageListener));
  }

  imageStream.addListener(ImageStreamListener(imageListener));
  return completer.future;
}
